<?php
	session_start();
	error_reporting(0);
	$invalid 	= '';
	if ( isset($_POST['commit']) ) {
		if ( $_POST['login_password'] != 'mseuf?p!' ) {
			$invalid = '&bull; Invalid Login Password';
		}
		else {
			$_SESSION['is_logged'] 		= true;
			$_SESSION['db_host'] 		= 'localhost';
			$_SESSION['db_username'] 	= $_POST['username'];
			$_SESSION['db_password'] 	= $_POST['password'];
		}
	}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Manuel S. Enverga University Foundataion - Database Installation</title>
	<link rel="stylesheet" href="../application/scripts/css/DB-Installation.css">
	<script type="text/javascript" src="../application/scripts/js/jquery-1.7.1.js"></script>
	<script type="text/javascript">
	
		var loader 	= '<img src="../application/images/icon/loading_round.gif" border="0" />';
		var check 	= '<span style="color:#090; font-size:11px; font-weight:bold;"><img src="../application/images/icon/checkbox_check.png" border="0" /> SUCCESS</span>';
		
		function home() {
			window.location.href = '../';
		}
		
		function logOut() {
			$.ajax({
				url: 'ajax.php',
				data: {ajax : 'logout'},
				dataType: 'JSON',
				type: 'POST',
				success: function(r) {
					if (r) {
						window.location.href = window.location.href;
					}
				}
			})
		}
		
		function createDB() {
			$('#success').html(loader);
			$.ajax({
				url: 'ajax.php',
				data: {ajax : 'create-db', host : '<?php echo $_SESSION['db_host']; ?>', username : '<?php echo $_SESSION['db_username']; ?>', password : '<?php echo $_SESSION['db_password']; ?>'},
				dataType: 'JSON',
				type: 'POST',
				success: function(r) {
					$('#success').text(r);
					setTimeout(function() {
						window.location.href = window.location.href;
					}, 1000)
				}
			})
		}
		
		function createTables() {
			$('.tbl-loader').html(loader);
			$('#activate-tables').html(loader + ' Proccessing. Please wait...');
			$.ajax({
				url: 'ajax.php',
				data: {ajax : 'create-tables', host : '<?php echo $_SESSION['db_host']; ?>', username : '<?php echo $_SESSION['db_username']; ?>', password : '<?php echo $_SESSION['db_password']; ?>'},
				dataType: 'JSON',
				type: 'POST',
				success: function(r) {
					$('#activate-tables').text(r);
					$('.tbl-loader').html('');
					$('.tbl-name').css('color', '#00F');
				}
			})
		}
		
	</script>
</head>
<body>
	<?php if ( !isset($_SESSION['is_logged']) ) { ?>
		<section class="container">
		<form method="POST" action="">
			<div class="login">
				<h1>DATABASE CONFIGURATION</h1>
				<p><input type="text" name="username" placeholder="Database Username"></p>
				<p><input type="text" name="password" placeholder="Database Password"></p>
			</div>
			<br />
			<div class="login">
				<h1>LOGIN</h1>
				<p><input type="password" name="login_password" placeholder="Login Password"></p>
				<p class="invalid"><?php echo $invalid; ?></p>
				<p class="submit"><input type="submit" name="commit" value="Login"></p>
			</div>
		</form>
		</section>
	<?php } else { ?>
		<div class="mseuf-name">Manuel S. Enverga University Foundation</div>
		<div><span onclick="home();" class="logout">[HOME]</span> &nbsp;|&nbsp; <span onclick="logOut();" class="logout">[LOGOUT]</span></div>
		
		<div style="margin-top:20px; font-size:18px; font-family:">
			>> DATABASE INSTALLATION <<
		</div>
		
		<div style="padding:20px; border: dotted 1px #ccc; margin-top:20px; ">
			<div style="font-size:18px; font-family:">
				:: <u>Connection</u>
			</div>
			<div style="padding-left:20px;">
				<table width="0" border="0" cellspacing="0" cellpadding="0" style="margin-top:5px;">
					<tr>
						<td align="left" width="100">
							<span>Host:</span>
						</td>
						<td align="left" width="80" colspan="3">
							 <strong style="color:#00F;"><?php echo $_SESSION['db_host']; ?></strong>
						</td>
					</tr>
					<tr>
						<td align="left">
							<span>Username:</span> 
						</td>
						<td align="left" colspan="3">
							 <strong style="color:#00F;"><?php echo $_SESSION['db_username']; ?></strong>
						</td>
					</tr>
					<tr>
						<td align="left">
							<span>Password:</span> 
						</td>
						<td align="left" colspan="3">
							 <strong style="color:#00F;"><?php echo $_SESSION['db_password']; ?></strong>
						</td>
					</tr>
					<tr>
						<td height="10" colspan="4"></td>
					</tr>
					<tr>
						<td align="left">
							<span>DB Name:</span>
						</td>
						<td align="left" width="90">
							<strong style="color:#00F;"><em>dbmseuf</em></strong>
						</td>
						<td>
							<?php
								$conn = mysql_connect($_SESSION['db_host'], $_SESSION['db_username'], $_SESSION['db_password']);
								if( !$conn ) { echo "Could not connect to MySQL Connection"; }
								else {
									$retval = mysql_select_db('dbmseuf');
									echo (! $retval ) ? "<span class=\"install\" onclick=\"createDB()\">INSTALL</span>&nbsp;&nbsp;&nbsp;" : "<span class=\"okay\">Database  Created</span>";
									mysql_close($conn);
								}
							?>
						</td>
						<td><span id="success"></span></td>
					</tr>
					
				</table>
			</div>
		</div>
		<?php
			$conn = mysql_connect($_SESSION['db_host'], $_SESSION['db_username'], $_SESSION['db_password']);
			if( $conn ) {
				$retval = mysql_select_db('dbmseuf');
				if ( $retval ) {
					$arr = array('tbl_access', 'tbl_announcement', 'tbl_convertion', 'tbl_courses', 'tbl_department', 'tbl_fees', 'tbl_header', 'tbl_payments', 'tbl_prof_subj', 'tbl_prof_subj_sched', 'tbl_requisite', 'tbl_rooms', 'tbl_sched_time', 'tbl_sched_time_temp', 'tbl_section', 'tbl_sem_fees_linker', 'tbl_sem_linker', 'tbl_semesters', 'tbl_standing', 'tbl_stud_change_section', 'tbl_stud_change_section_irreg', 'tbl_stud_drop_subj_irreg', 'tbl_stud_request', 'tbl_stud_sec', 'tbl_stud_sem', 'tbl_stud_subj_fees', 'tbl_stud_subj_linker', 'tbl_students', 'tbl_subject', 'tbl_users_account', 'tbl_year');
					$countArr = count($arr);
					$tables = mysql_query("SHOW tables");
					$table_arr = array();
					while($row = mysql_fetch_assoc($tables)){
						$table_arr[] = $row['Tables_in_dbmseuf'];
					}
					$countTableArr = count($table_arr);
					$accesTbl = ( $countTableArr != $countArr ) ? '&nbsp;&nbsp;&nbsp;<span id="activate-tables" style="color:#F00;"><span class="install" onclick="createTables();">ACTIVATE TABLES</span></span>' : '';
					?>
						<div style="padding:20px; border: dotted 1px #ccc; margin-top:20px; ">
							<div style="font-size:18px; font-family:">
								:: <u>Database Tables</u><?php echo $accesTbl; ?>
							</div>
							<div style="padding-left:20px;">
								<table width="0" border="0" cellspacing="0" cellpadding="0" style="margin-top:5px;">
									<?php
										foreach( $arr as $res ) {
											$class_table = ( !in_array($res, $table_arr) ) ? 'disable' : 'enable';
											?>
												<tr>
													<td align="left" width="20"><div class="tbl-loader" id="tbl-loader-<?php echo $res; ?>"></div></td>
													<td align="left" width="240" style="border-bottom:1px dotted #999"><strong class="<?php echo $class_table; ?> tbl-name">&bull; <?php echo $res; ?></strong></td>
												</tr>
											<?php
										}
									?>
								</table>
							</div>
						</div>
					<?php
				}
				mysql_close($conn);
			}
		?>
	<?php } ?>
</body>
</html>